#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from common.common import exter_attack, output_result_file
from common.common_models import ActionResult
from common.const import ExecuteResultEnum
from hcs.gaussdb.common.const import EnvName
from hcs.gaussdb.common.safe_get_information import ResourceParam
from hcs.gaussdb.handle.restore.exec_restore import Restore
from hcs.gaussdb.handle.restore.gaussdb_restore_service import GaussDBRestoreService


class RestoreAbility:
    @staticmethod
    @exter_attack
    def restore(req_id, job_id, sub_id, data):
        output = ActionResult(code=ExecuteResultEnum.SUCCESS)
        EnvName.IAM_USERNAME = "job_targetEnv_auth_authKey"
        EnvName.IAM_PASSWORD = "job_targetEnv_auth_authPwd"
        param_inst = ResourceParam(req_id)
        param = param_inst.get_param()
        backup_inst = Restore(req_id, job_id, sub_id, data, param)
        result = backup_inst.restore_task()
        if not result:
            output.code = ExecuteResultEnum.INTERNAL_ERROR
        output_result_file(req_id, output.dict(by_alias=True))
        return ExecuteResultEnum.SUCCESS

    @staticmethod
    @exter_attack
    def allow_restore_in_local_node(req_id, job_id, sub_id, data):
        return GaussDBRestoreService.allow_restore_in_local_node(req_id, job_id, sub_id)

    @staticmethod
    @exter_attack
    def allow_restore_sub_job_in_local_node(req_id, job_id, sub_id, data):
        return GaussDBRestoreService.allow_restore_in_local_node(req_id, job_id, sub_id)

    @staticmethod
    @exter_attack
    def restore_prerequisite(req_id, job_id, sub_id, data):
        return GaussDBRestoreService.restore_pre_job(req_id, job_id, sub_id, data)

    @staticmethod
    @exter_attack
    def restore_prerequisite_progress(req_id, job_id, sub_id, data):
        return GaussDBRestoreService.restore_prerequisite_progress(req_id, job_id, sub_id, data)

    @staticmethod
    @exter_attack
    def restore_gen_sub_job(req_id, job_id, sub_id, data):
        return GaussDBRestoreService.restore_gen_sub_job(req_id, job_id, sub_id, data)

    @staticmethod
    @exter_attack
    def restore_progress(req_id, job_id, sub_id, data):
        return GaussDBRestoreService.restore_progress(req_id, job_id, sub_id, data)

    @staticmethod
    @exter_attack
    def restore_post_job(req_id, job_id, sub_id, data):
        return GaussDBRestoreService.restore_post_job(req_id, job_id, sub_id, data)

    @staticmethod
    @exter_attack
    def restore_post_job_progress(req_id, job_id, sub_id, data):
        return GaussDBRestoreService.restore_post_job_progress(req_id, job_id, sub_id, data)
